package com.accompany.controller;

import com.accompany.common.Result;
import com.accompany.entity.Game;
import com.accompany.service.GameService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 游戏表 前端控制器
 * </p>
 *
 * @author DC
 * @since 2024-12-26
 */
@RestController
@RequestMapping("/games")
@Tag(name = "GameController", description = "游戏管理")
public class GameController {

    @Autowired
    private GameService gameService;

    /**
     * 创建游戏
     *
     * @param game 游戏信息
     * @return Result对象，包含操作结果
     */
    @PostMapping("/create")
    @Operation(summary = "创建游戏", description = "创建一个新的游戏")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "游戏创建成功"),
            @ApiResponse(responseCode = "400", description = "请求参数不正确")
    })
    public Result<?> createGame(@RequestBody Game game) {
        gameService.createGame(game);
        return Result.success();
    }

    /**
     * 删除游戏
     *
     * @param id 游戏ID
     * @return Result对象，包含操作结果
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除游戏", description = "根据游戏ID删除游戏")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "游戏删除成功"),
            @ApiResponse(responseCode = "404", description = "游戏不存在")
    })
    public Result<?> deleteGame(@PathVariable Long id) {
        gameService.deleteGame(id);
        return Result.success("删除成功");
    }

    /**
     * 更新游戏信息
     *
     * @param game 游戏信息
     * @return Result对象，包含操作结果
     */
    @PutMapping("/update")
    @Operation(summary = "更新游戏信息", description = "根据游戏ID更新游戏信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "游戏信息更新成功"),
            @ApiResponse(responseCode = "404", description = "游戏不存在")
    })
    public Result<?> updateGame(@RequestBody Game game) {
        gameService.updateGame(game);
        return Result.success("更新成功");
    }

    /**
     * 获取单个游戏信息
     *
     * @param id 游戏ID
     * @return Result对象，包含游戏信息
     */
    @GetMapping("/{id}")
    @Operation(summary = "获取单个游戏信息", description = "根据游戏ID获取游戏信息")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "404", description = "游戏不存在")
    })
    public Result<Game> getGameById(@PathVariable Long id) {
        Game game = gameService.getGameById(id);
        return Result.success(game);
    }

    /**
     * 获取所有游戏列表
     *
     * @return Result对象，包含游戏列表
     */
    @GetMapping
    @Operation(summary = "获取所有游戏列表", description = "获取所有游戏的列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "获取成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<List<Game>> getAllGames() {
        List<Game> games = gameService.getAllGames();
        return Result.success(games);
    }

    /**
     * 分页查询游戏列表
     *
     * @param page  页码
     * @param size  每页大小
     * @return Result对象，包含分页后的游戏列表
     */
    @GetMapping("/page")
    @Operation(summary = "分页查询游戏列表", description = "根据页码和每页大小分页查询游戏列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "分页查询成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    public Result<Page<Game>> getPaginatedGames(
            @Parameter(description = "页码", required = true) @RequestParam(name = "page", defaultValue = "1") Integer page,
            @Parameter(description = "每页大小", required = true) @RequestParam(name = "size", defaultValue = "10") Integer size) {
        Page<Game> pageResult = gameService.getPaginatedGames(page, size);
        return Result.success(pageResult);
    }
}